#include <stdio.h>
#include <stdlib.h>

char* mitoa(int, char*, short int);

int main() {
	char* str = (char*)malloc(1);

	if (!str) return -1;
	str[0] = '\0';
	str = mitoa(103, str, 0);
	printf("103 to str: %s\n", str);

	free(str);
	return 0;
}


char* mitoa(int n, char* str, short int strLength) {
	int unit = n % 10;
	char* newStr = (char*)malloc(0);
	
	n /= 10;
	newStr = (char*)realloc(str, (__int64)strLength + 2);

	if (newStr) {
		for (int i = (__int64)strLength; i > 0; i--)
		{
			newStr[i] = newStr[i - 1];
		}
		newStr[strLength + 1] = '\0';
		newStr[0] = (char)(unit + '0');

		if (n != 0) {
			return mitoa(n, newStr, (__int64)strLength + 1);
		}
		else {
			return newStr;
		}
	}
	else {
		return str;
	}
}